﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActiveBatch;

namespace AbatDeployer
{
    class AbatHelper
    {

        public void localizeVariable(AbatModel abatModel,string varPath, AbatJobScheduler scheduler)
        {
            
              
            try
            {
                
                ActiveBatch.IFolder abatJob = (ActiveBatch.IFolder)scheduler.GetAbatObject(varPath);

                //Get the variables from that object
                ActiveBatch.AbatVariables abatVariables = abatJob.GetVariables();

                foreach (Object var in abatVariables)
                {
                    if (var is AbatVariable)
                    {
                        //perform operation of comapring and changing the value
                        AbatVariable Tvar = var as AbatVariable;

                        ABATVariable mappedVar = abatModel.VariblesList.Select(obj => obj).Where(obj => obj.Name.Equals(Tvar.Name)).FirstOrDefault();
                        if (mappedVar != null)
                        {
                            //Set the variable named test2 with the value "NewValue"
                            abatVariables.SetVariable(Tvar.Name, mappedVar.value, mappedVar.desc, false);
                        }

                        else
                        {
                            //print the log for not mapped object 

                        }

                    }

                    else
                    {
                        //print the log for undentified object found that cant be localized


                    }

                }

                //Update the job so the changes are saved 
                abatJob.Update();

                //Print information about the job
                Console.WriteLine(abatJob.Name);
                Console.WriteLine(abatJob.ID);
            }

            catch (Exception e)
            {
                throw e;
            }

        }



        public void localizeQueues(AbatModel abatModel, string varPath, AbatJobScheduler scheduler)
        {

            try
            {
                AbatObjectsLite queuelist = scheduler.GetAbatObjectsLite(varPath, 12);
                foreach (object queueO in queuelist)
                {
                    if (queueO is AbatQueueLite)
                    {

                        AbatQueueLite Casted_Q = queueO as AbatQueueLite;
                        ABATQueue mappedQ = abatModel.Queuelist.Where(t => t.name.Equals(Casted_Q.Name)).SingleOrDefault();

                        if (mappedQ != null)
                        {
                            ActiveBatch.Queue50 pop = (ActiveBatch.Queue50)scheduler.GetAbatObject(Casted_Q.ID);
                            pop.ExecutionMachine = mappedQ.ExecutionMachine;
                            pop.Update();
                        }

                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        public void localize_serviceAccount(AbatModel abat, string varPath, AbatJobScheduler scheduler)
        {

      
            try
            {
                AbatObjectsLite serviceList = scheduler.GetAbatObjectsLite(varPath, 64);


                foreach (object saO in serviceList)
                {
                    if (saO is AbatUserAccountLite)
                    {

                        AbatUserAccountLite Casted_Q = saO as AbatUserAccountLite;



                        ABATUserAccount mappedQ = abat.UserAccountList.Where(t => t.name.Equals(Casted_Q.Name)).SingleOrDefault();


                        if (mappedQ != null)
                        {
                            ActiveBatch.UserAccount50 pop = (ActiveBatch.UserAccount50)scheduler.GetAbatObject(Casted_Q.ID);
                            pop.Username = mappedQ.UserName;
                            pop.Password = mappedQ.PassWord;
                            pop.Update();
                        }

                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }

        }




        public void localize_serviceReference(AbatModel abat, string VarPath, AbatJobScheduler scheduler)
        {

            
            try{
            AbatObjectsLite serviceList = scheduler.GetAbatObjectsLite(VarPath, 1024);


            foreach (object srvO in serviceList)
            {
                if (srvO is ServiceLibraryLite)
                {

                    ServiceLibraryLite Casted_Q = srvO as ServiceLibraryLite;

                    ABATServiceR mappedQ = abat.SrvAccountList.Where(t => t.name.Equals(Casted_Q.Name)).SingleOrDefault();
                    if (mappedQ != null)
                    {
                        ActiveBatch.ServiceLibrary srvLib = (ActiveBatch.ServiceLibrary)scheduler.GetAbatObject(Casted_Q.ID);

                        foreach (AbatVariable var in srvLib.Properties)
                        {

                            if (var.Name.Equals("Configuration"))
                            {
                                AbatVariables configVar = var.Value as AbatVariables;

                                configVar.SetVariable("WsdlLocation", "", mappedQ.wsdl, false);

                            }
                        }
                        srvLib.Update();
                    }


                }
            }


            }

            catch (Exception e)
            {
                throw e;
            }

        }



    }
}
